package com.example.thai.mydemo2.SqlAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.thai.mydemo2.R;
import com.example.thai.mydemo2.SqlDAO.UserDAO;

import java.util.ArrayList;


/**
 * Created by thai on 2014/10/28.
 * BaseAdapter控制ListView畫面
 */
public class MyAdapter extends BaseAdapter {
    ArrayList<ApkEntity> apk_list;
    private Activity parentActivity;
    private Context context;
    LayoutInflater inflater;
    private int seletedMode;// 使用者選擇的編輯模式
    private int seletedIndex;// 使用者選擇的列
    private String[] modeList = { "編輯", "刪除" };
    private int siz1;
    private  static int siz2;


    public MyAdapter(Context context, ArrayList<ApkEntity> apk_list) {
        this.apk_list = apk_list;
        this.inflater = LayoutInflater.from(context);


    }

    public int getViewTypeCount() {
        //Log.v(TAG, "in getViewTypeCount()");
        return 1;
    }

    public int getItemViewType(int position) {
        //Log.v(TAG, "in getItemViewType() for position " + position);
        return 0;
    }

//      控制一次呈現的數量
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        siz1 = apk_list.size();
//        Log.i("TAG","Siz1 = " + siz1);
        return siz1;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return apk_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
//        Log.i("TAG","getItemId = " + position);
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
//        Log.v(TAG, "in getView for position " + position +
//                ", convertView is " +
//                ((convertView == null)?"null":"being recycled"));
        final Context context = parent.getContext();
        final ApkEntity entity = apk_list.get(position);
        ViewHolder holder;
        final UserDAO userDAO = new UserDAO(context);


//      儲存畫面上的元件holder
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_layout, null);

            holder.img_im = (ImageView) convertView
                    .findViewById(R.id.item3_apkiv);
            holder.name_tv = (TextView) convertView
                    .findViewById(R.id.item3_apkname);
            holder.des_tv = (TextView) convertView
                    .findViewById(R.id.item3_apkdes);
            holder.info_tv = (TextView) convertView
                    .findViewById(R.id.item3_apkinfo);
            holder.bnt_ib = (Button) convertView
                    .findViewById(R.id.item3_dlbtn);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

//      刪除元件的邏輯與畫面上的文字
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        alertDialog.setTitle("刪除");
        alertDialog.setMessage("你確定要刪除嗎???");
        holder.bnt_ib.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // 刪除陣列上的項目
                        apk_list.remove(position);
                        //暫時
                        // 通知UI重新整理畫面上的呈現
                        notifyDataSetChanged();
                        
                    }
                });
                alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // 當選擇NO的處理
                        // 目前是不處理
                    }
                });
                alertDialog.setIcon(android.R.drawable.btn_plus);
                // 執行刪除元件的畫面呈現
                alertDialog.show();
            }
        });

            //處理如果使用者沒有輸入照片就使用預設的照片
            if(entity.getImage() == null)
            {
                holder.img_im.setImageResource(entity.getIntImg());
            }
        else
            {
                holder.img_im.setImageBitmap(entity.getImage());
            }
            //Log.i("TAG","position = " + position);
            holder.name_tv.setText(entity.getName());
            holder.des_tv.setText(entity.getInfo());
            holder.info_tv.setText("position" + position);

            return convertView;
    }




    //UI元件的控制器初始化
    class ViewHolder {
        ImageView img_im;
        TextView name_tv;
        TextView des_tv;
        TextView info_tv;
        Button bnt_ib;
        LinearLayout lv_lv;
    }
}